namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLineEllipse : PaintLineBase
    {
        public PaintLineEllipse()
        {
            base.LineType = PaintLineType.Ellipse;
            base.PlName_zh = "标注圆";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                PaintLineBase.PointValue value2 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                PaintLineBase.PointValue value3 = base.PointList[1];
                int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num4 = base.CgInstance.GetX(num2);
                PaintLineBase.PointValue value4 = base.PointList[0];
                float num5 = base.CgInstance.GetY(base.OwnedPanelID, value4.Value, null);
                PaintLineBase.PointValue value5 = base.PointList[1];
                float num6 = base.CgInstance.GetY(base.OwnedPanelID, value5.Value, null);
                if (((crossHairPoint.X >= (x - (base.LineWidth * 5))) && (crossHairPoint.X <= (x + (base.LineWidth * 5)))) && ((crossHairPoint.Y >= (num5 - (base.LineWidth * 5))) && (crossHairPoint.Y <= (num5 + (base.LineWidth * 5)))))
                {
                    base.CgInstance.Cursor = Cursors.SizeNS;
                    base.MoveType = 1;
                }
                else if (((crossHairPoint.X >= (num4 - (base.LineWidth * 5))) && (crossHairPoint.X <= (num4 + (base.LineWidth * 5)))) && ((crossHairPoint.Y >= (num6 - (base.LineWidth * 5))) && (crossHairPoint.Y <= (num6 + (base.LineWidth * 5)))))
                {
                    base.CgInstance.Cursor = Cursors.SizeWE;
                    base.MoveType = 2;
                }
                else
                {
                    base.MoveType = 0;
                    base.CgInstance.Cursor = Cursors.Hand;
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                g.SmoothingMode = SmoothingMode.AntiAlias;
                PaintLineBase.PointValue value2 = pList[0];
                double chartValue = value2.Value;
                PaintLineBase.PointValue value3 = pList[1];
                double num2 = value3.Value;
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float num5 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                float num6 = base.CgInstance.GetX(num4) - base.CgInstance.LeftYScaleWidth;
                float num7 = (base.CgInstance.GetY(base.OwnedPanelID, chartValue, null) - div.Rect.Y) - div.TitleHeight;
                float num8 = (base.CgInstance.GetY(base.OwnedPanelID, num2, null) - div.Rect.Y) - div.TitleHeight;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                float x = num5 - (num5 - num6);
                float y = 0f;
                float width = (num5 - num6) * 2f;
                float height = 0f;
                if (num7 >= num8)
                {
                    y = num7;
                    height = (num8 - num7) * 2f;
                }
                else
                {
                    y = num7 - ((num7 - num8) * 2f);
                    height = (num7 - num8) * 2f;
                }
                if (width == 0f)
                {
                    width = 1f;
                }
                if (height == 0f)
                {
                    height = 1f;
                }
                if ((width == 1f) && (height == 1f))
                {
                    RectangleF rect = new RectangleF(x - 2f, y - 2f, 4f, 4f);
                    g.DrawEllipse(pen, rect);
                }
                else
                {
                    RectangleF ef2 = new RectangleF(x, y, width, height);
                    g.DrawEllipse(pen, ef2);
                }
                if (base.Selected)
                {
                    g.FillRectangle(brush, new RectangleF(num5 - (base.LineWidth * 2.5f), num7 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(num6 - (base.LineWidth * 2.5f), num8 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                brush.Dispose();
                pen.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int indexByPoint = base.CgInstance.GetIndexByPoint(mp);
                double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                string key = base.CgInstance.DataSource.Rows[indexByPoint][base.CgInstance.CalenderScaleField].ToString();
                base.PointList.Clear();
                base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                base.PointList.Add(new PaintLineBase.PointValue(1, key, valueByPoint));
                return true;
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count == 0)
            {
                return false;
            }
            Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
            PaintLineBase.PointValue value2 = base.PointList[0];
            double chartValue = value2.Value;
            PaintLineBase.PointValue value3 = base.PointList[1];
            double num2 = value3.Value;
            PaintLineBase.PointValue value4 = base.PointList[0];
            int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
            PaintLineBase.PointValue value5 = base.PointList[1];
            int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
            float x = base.CgInstance.GetX(pos);
            float num6 = base.CgInstance.GetX(num4);
            float num7 = base.CgInstance.GetY(base.OwnedPanelID, chartValue, null);
            float num8 = base.CgInstance.GetY(base.OwnedPanelID, num2, null);
            float num9 = 0f;
            float num10 = 0f;
            if (x >= num6)
            {
                num9 = num6;
            }
            else
            {
                num9 = num6 - ((num6 - x) * 2f);
            }
            if (num7 >= num8)
            {
                num10 = num7 - ((num7 - num8) * 2f);
            }
            else
            {
                num10 = num7;
            }
            float num11 = Math.Abs((float) ((x - num6) * 2f));
            float num12 = Math.Abs((float) ((num7 - num8) * 2f));
            float num13 = num9 + (num11 / 2f);
            float num14 = num10 + (num12 / 2f);
            double num15 = this.LineParam()[0];
            double num16 = this.LineParam()[1];
            if ((num15 == -1.0) && (num16 == -1.0))
            {
                RectangleF ef = new RectangleF(num9 - (base.LineWidth * 2), num10 - (base.LineWidth * 2), num11 + (base.LineWidth * 4), num12 + (base.LineWidth * 4));
                return ef.Contains((PointF) crossHairPoint);
            }
            float num17 = crossHairPoint.X - num13;
            float num18 = crossHairPoint.Y - num14;
            double num19 = (((double) (num17 * num17)) / (num15 * num15)) + (((double) (num18 * num18)) / (num16 * num16));
            return ((num19 >= 0.8) && (num19 <= 1.2));
        }

        public override double[] LineParam()
        {
            if (base.PointList.Count == 0)
            {
                return null;
            }
            PaintLineBase.PointValue value2 = base.PointList[0];
            int pos = base.CgInstance.GetRecordByTime(value2.Key) - 1;
            PaintLineBase.PointValue value3 = base.PointList[1];
            int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
            float x = base.CgInstance.GetX(pos);
            float num4 = base.CgInstance.GetX(num2);
            PaintLineBase.PointValue value4 = base.PointList[0];
            float num5 = base.CgInstance.GetY(base.OwnedPanelID, value4.Value, null);
            PaintLineBase.PointValue value5 = base.PointList[1];
            float num6 = base.CgInstance.GetY(base.OwnedPanelID, value5.Value, null);
            double num7 = x - num4;
            double num8 = num5 - num6;
            if ((num7 == 0.0) || (num8 == 0.0))
            {
                return new double[] { -1.0, -1.0 };
            }
            double num9 = Math.Sqrt(num7 * num7);
            double num10 = Math.Sqrt(num8 * num8);
            return new double[] { num9, num10 };
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num3 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num4 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num5 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num6 = (num3 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num5 - num4);
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num8 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                            int num9 = (base.CgInstance.GetRecordByTime(value2.Key) + (num8 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value3 = base.BeginMoveValue[1];
                            int num10 = (base.CgInstance.GetRecordByTime(value3.Key) + (num8 - indexByPoint)) - 1;
                            if (num9 >= 0)
                            {
                                if (num9 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num9 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num9 = 0;
                            }
                            if (num10 < 0)
                            {
                                num10 = 0;
                            }
                            else if (num10 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num10 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num9][base.CgInstance.CalenderScaleField].ToString(), value4.Value + num6);
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num10][base.CgInstance.CalenderScaleField].ToString(), value5.Value + num6);
                            return;
                        }
                        case 1:
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            return;

                        case 2:
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            return;
                    }
                }
            }
        }
    }
}

